package com.example.demo.controller;

import com.example.demo.componet.ReadCSV;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 男女季节季节差异
 *
 * @author xwh
 * @date 2023/02/05
 */
@RestController
@RequestMapping("/analyze")
public class SeasonDifference {

    @Resource
    private JavaSparkContext sc;

    //文件地址
    @Autowired
    private String getFilePath;


    private List<ArrayList> getGetFilePath(){
        ReadCSV readCSV = new ReadCSV();
        List<ArrayList> dataList = readCSV.readThreeColumn(getFilePath, 1,7,10);  //"C:\\Users\\fireapproval\\Desktop\\毕业设计\\数据集\\test.csv"
        return dataList;
    }




    @GetMapping("/seasonDifference")
    public List<ArrayList> method1(){

        //"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"

        //System.out.println(dataList);

        //每个月男女消费记录

        ArrayList<Float> manJan = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manFeb = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manMar = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manApr = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manMay = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manJun = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manJul = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manAug = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manSep = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manOct = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manNov = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> manDec = new ArrayList<>(Arrays.asList(0f,0f));

        ArrayList<Float> womanJan = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanFeb = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanMar = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanApr = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanMay = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanJun = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanJul = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanAug = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanSep = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanOct = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanNov = new ArrayList<>(Arrays.asList(0f,0f));
        ArrayList<Float> womanDec = new ArrayList<>(Arrays.asList(0f,0f));


        //System.out.println(manApr);

        //System.out.println(womanApr);

        for (Object obj:getGetFilePath()
        ) {
            ArrayList a = (ArrayList) obj;
            //性别
            String s = (String) a.get(2);
            //消费
            Float f = Float.parseFloat((String) a.get(1));
            //月份
            Integer i = Integer.parseInt((String) a.get(0));


            if ((s.equals("男") || s == "男") && (i.intValue() == 1)){
                manJan.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 1)){
                womanJan.add(f);
            }
            //2
            if ((s.equals("男") || s == "男") && (i.intValue() == 2)){
                manFeb.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 2)){
                womanFeb.add(f);
            }
            //3
            if ((s.equals("男") || s == "男") && (i.intValue() == 3)){
                manMar.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 3)){
                womanMar.add(f);
            }
            //4
            if ((s.equals("男") || s == "男") && (i.intValue() == 4)){
                manApr.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 4)){
                womanApr.add(f);
            }
            //5
            if ((s.equals("男") || s == "男") && (i.intValue() == 5)){
                manMay.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 5)){
                womanMay.add(f);
            }
            //6
            if ((s.equals("男") || s == "男") && (i.intValue() == 6)){
                manJun.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 6)){
                womanJun.add(f);
            }
            //6
            if ((s.equals("男") || s == "男") && (i.intValue() == 7)){
                manJul.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 7)){
                womanJul.add(f);
            }

            if ((s.equals("男") || s == "男") && (i.intValue() == 8)){
                manAug.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 8)){
                womanAug.add(f);
            }
            //9
            if ((s.equals("男") || s == "男") && (i.intValue() == 9)){
                manSep.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 9)){
                womanSep.add(f);
            }

            if ((s.equals("男") || s == "男") && (i.intValue() == 10)){
                manOct.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 10)){
                womanOct.add(f);
            }

            if ((s.equals("男") || s == "男") && (i.intValue() == 11)){
                manNov.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 11)){
                womanNov.add(f);
            }

            if ((s.equals("男") || s == "男") && (i.intValue() == 12)){
                manDec.add(f);
            }
            if ((s.equals("女") || s == "女") && (i.intValue() == 12)){
                womanDec.add(f);
            }

        }

        System.out.println(manApr);
        System.out.println(womanApr);


        JavaRDD<Float> one = sc.parallelize(manJan);
        JavaRDD<Float> two = sc.parallelize(manFeb);
        JavaRDD<Float> three = sc.parallelize(manMar);
        JavaRDD<Float> four = sc.parallelize(manApr);
        JavaRDD<Float> five = sc.parallelize(manMay);
        JavaRDD<Float> six = sc.parallelize(manJun);
        JavaRDD<Float> seven = sc.parallelize(manJul);
        JavaRDD<Float> eight = sc.parallelize(manOct);
        JavaRDD<Float> night = sc.parallelize(manSep);
        JavaRDD<Float> ten = sc.parallelize(manOct);
        JavaRDD<Float> eleven = sc.parallelize(manNov);
        JavaRDD<Float> twelve = sc.parallelize(manDec);

        //女12
        JavaRDD<Float> wone = sc.parallelize(womanJan);
        JavaRDD<Float> wtwo = sc.parallelize(womanFeb);
        JavaRDD<Float> wthree = sc.parallelize(womanMar);
        JavaRDD<Float> wfour = sc.parallelize(womanApr);
        JavaRDD<Float> wfive = sc.parallelize(womanMay);
        JavaRDD<Float> wsix = sc.parallelize(womanJun);
        JavaRDD<Float> wseven = sc.parallelize(womanJul);
        JavaRDD<Float> weight = sc.parallelize(womanOct);
        JavaRDD<Float> wnight = sc.parallelize(womanSep);
        JavaRDD<Float> wten = sc.parallelize(womanOct);
        JavaRDD<Float> weleven = sc.parallelize(womanNov);
        JavaRDD<Float> wtwelve = sc.parallelize(womanDec);


        //男12个
        Float mreduce = one.reduce((Function2<Float, Float, Float>) (aFloat, aFloat2) -> aFloat+aFloat);
        Float mreduce2 = two.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce3 = three.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce4 = four.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce5 = five.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce6 = six.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce7 = seven.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce8 = eight.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce9 = night.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce10 = ten.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce11 = eleven.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float mreduce12 = twelve.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);

        //女12
        Float wreduce = wone.reduce((Function2<Float, Float, Float>) (aFloat, aFloat2) -> aFloat+aFloat2);
        Float wreduce2 = wtwo.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce3 = wthree.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce4 = wfour.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce5 = wfive.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce6 = wsix.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce7 = wseven.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce8 = weight.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce9 = wnight.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce10 = wten.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce11 = weleven.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);
        Float wreduce12 = wtwelve.reduce((Function2<Float, Float, Float>) (integer, integer2) -> integer + integer2);

        ArrayList<Float> manData = new ArrayList<>();
        ArrayList<Float> womanData = new ArrayList<>();

        manData.add(mreduce);manData.add(mreduce2);manData.add(mreduce3);manData.add(mreduce4);manData.add(mreduce5);manData.add(mreduce6);manData.add(mreduce7);manData.add(mreduce8);manData.add(mreduce9);manData.add(mreduce10);manData.add(mreduce11);manData.add(mreduce12);
        womanData.add(wreduce);womanData.add(wreduce2);womanData.add(wreduce3);womanData.add(wreduce4);womanData.add(wreduce5);womanData.add(wreduce6);womanData.add(wreduce7);womanData.add(wreduce8);womanData.add(wreduce9);womanData.add(wreduce10);womanData.add(wreduce11);womanData.add(wreduce12);

        List<ArrayList> returnData = new ArrayList<>();

        returnData.add(manData);returnData.add(womanData);

        return returnData;
    }
}
